package ch.unisi.inf.java.project.riskbami.model;

/**
 * PlaceArmiesCommand is an extension of GenericCommand.
 * Provide the implementation of a commands that is responsible to place the
 * available armies for this turn
 * 
 * @author minellir@lu.unisi.ch
 * @author babazadm@lu.unisi.ch
 */
public final class PlaceArmiesCommand extends GenericCommand
{

    private final Player player;
    private final Country country;
    private final int howMany;
    
    /**
     * The constructor for the class PlaceArmiesCommand
     * 
     * @param game The actual game
     * @param country The country in which you would place armies
     * @param howMany The number of armies that you would like to place
     */
    public PlaceArmiesCommand(final Game game, final Country country, final int howMany) {
        super(game);
        this.player = game.getCurrentPlayer();
        this.country = country;
        this.howMany = howMany;
    }

    /**
     * canExecute verify whether the PlaceArmiesCommand could be executed
     * 
     * @return boolean
     */
    public final boolean canExecute() {
        if(game.getTurnPhase() == 1) {
            return true;
        }
        else return false;
    }
    
    /**
     * execute is responsible to execute the given PlaceArmiesCommand.
     */
    public final void execute(){
        final int armies = player.getPerTurnArmies();
        if(player.getOwnedCountries().contains(country) && howMany <= armies){
                country.addArmies(howMany);
                player.removePerTurnArmies(howMany);
                
            if(player.isInitialPlacing()){
                if(player.getPerTurnArmies() == 0){
                     player.noLongerInitialPlacing();
                    }
                game.switchNextPlayer();
                return;
            }
            
            if(player.getPerTurnArmies() == 0){
                 game.switchNextPhase();
             }
         }
    }
    
}
